import path from "path"
import fs from "fs-extra"
import os from "os"
import * as glob from "glob"
import AdmZip from "adm-zip"
import puppeteer from "puppeteer-core"
import { desktopPath as desktop, setNetInterface } from "@/utils"
import { ITask } from "@/interface"

const download = async (url: string) => {
  const data = <ITask>fs.readJSONSync("../../../env.duty.json")

  const [{ ip }] = data.ipMap.filter(i => i.isMaster)

  await setNetInterface(ip)

  const browser = await puppeteer.launch({
    headless: true,
    channel: "chrome",
    defaultViewport: null,
    userDataDir: path.join(".cache", ip),

    args: [
      "--disable-gpu",
      "--no-zygote",
      // "--disable-dev-shm-usage",
      // "--no-first-run",
      // "--single-process",

      "--no-sandbox",
      "--disable-setuid-sandbox",
      "--disable-notifications",
      "--disable-extensions",
    ],
  })

  const page = await browser.newPage()

  await page.goto(url, { waitUntil: "load" })

  await Promise.all([
    page.addScriptTag({
      url: "https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js",
      id: "jquery",
    }),

    page.waitForFunction(() => window["jQuery"] !== undefined, {
      timeout: 0,
    }),
  ])

  await page.waitForTimeout(2000)

  return new Promise(async (resolve, reject) => {
    page.on("error", reject)

    const loginBtn = await page.evaluate(
      () => $(".text:contains(登录UC网盘)").length,
    )

    console.log("AT-[ loginBtn &&&&&********** ]", loginBtn)

    if (loginBtn) {
      await page.locator("div::-p-text(登录UC网盘)").wait()

      await page.locator("div::-p-text(登录UC网盘)").click()

      await page.waitForSelector(".iframeShow")

      await page.waitForTimeout(5000)

      const binary = await page.screenshot({ encoding: "binary" })

      resolve(binary)
    } else {
      const cdpSession = await page.target().createCDPSession()

      const downloadPath = fs.mkdtempSync(path.join(os.tmpdir(), "zip-"))

      fs.ensureDirSync(downloadPath)

      await cdpSession.send("Page.setDownloadBehavior", {
        behavior: "allow", //允许所有下载请求
        downloadPath, //设置下载路径
      })

      // 监听页面的 response 事件
      page.on("response", async response => {
        const url = response.request().url()

        if (url.includes("pds.uc.cn")) {
          const contentDisposition = response.headers()["content-disposition"]

          if (contentDisposition) {
            const filename = decodeURIComponent(
              contentDisposition.split("filename*=utf-8''")[1],
            )

            const watcher = fs.watch(downloadPath)

            watcher.on("change", (event, f) => {
              if (f === filename) {
                console.log("下载成功")

                const zipFilePath = path.join(downloadPath, filename)

                const zip = new AdmZip(zipFilePath)

                const tmpDir = path.join(downloadPath, "tmpDir")

                zip.extractAllTo(tmpDir, true)

                const extractedDir = glob.sync(path.join(tmpDir, "*/*/"), {
                  windowsPathsNoEscape: true,
                  ignore: {
                    ignored: p => p.name.includes("__MACOSX"),
                  },
                })

                extractedDir.forEach(dir => {
                  if (!dir.includes("__MACOSX")) {
                    fs.moveSync(dir, dir.replace(tmpDir, desktop), {
                      overwrite: true,
                    })
                  }
                })

                fs.rm(tmpDir, { recursive: true })

                watcher.close()

                page.close()
              }
            })
          }
        }
      })

      await page.locator(".task-footer-down").click()

      resolve("ok了")
    }
  })
}

export default download
